package code1.backtracking;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 原题链接：https://leetcode.cn/problems/combinations/
 */
public class P77 {


    List<List<Integer>> result= new ArrayList<>();

    LinkedList<Integer> path=new LinkedList<>();

    public void backtracking(int n,int k,int begin){
        if(path.size()==k){
            result.add(new ArrayList<>(path));
            return;
        }
        for(int i=begin;i<=n;i++){
            path.add(i);
            backtracking(n,k,i+1);
            path.removeLast();      //回溯
        }
    }

    public List<List<Integer>> combine(int n, int k) {
        backtracking(n,k,1);
        return result;
    }
}
